﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Web;
using DHQGHN;
using DHQGHN.DAL;
using DHQGHN.DAL.BO.TDT;
using DHQGHN.DAL.Models.TDT;

namespace DHQGHN.Services.TDT
{
    public class AreaEnumService : IAreaEnumService
    {
        private DAL.BO.TDT.AreaEnumeratorBO areaEnumeratorBO;
        log4net.ILog logger = log4net.LogManager.GetLogger(typeof(AreaEnumService));
        public AreaEnumService()
        {
            areaEnumeratorBO = new DAL.BO.TDT.AreaEnumeratorBO(DHQGHN.DAL.Common.ConnectionUtil.TKTHDS_TDT_CONNECTION_STRING);
        }

        public List<AreaEnumerator> GetAll()
        {
             try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                    return new List<AreaEnumerator>();
                }
                else
                {
                    return areaEnumeratorBO.GetAll();
                }
            }
            catch(Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                   new Fault.UnexpectedServiceFault
                   {
                       ErrorMessage = ex.Message,
                       Source = ex.Source,
                       Target = ex.TargetSite.ToString(),
                       StackTrace = ex.StackTrace
                   },
                   new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }

        public List<AreaEnumerator> GetAllPaging(string pageSize, string pageIndex)
        {
            throw new NotImplementedException();
        }

        public AreaEnumerator GetById(string areaEnumID)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                    return new AreaEnumerator();
                }
                else
                {
                    if (string.IsNullOrEmpty(areaEnumID))
                    {
                        Fault.ValidationFault fault = new Fault.ValidationFault()
                        {
                            Result = false,
                            Message = "Thiếu tham số",
                            Description = "Thiếu tham số"
                        };
                        logger.Error("areaEnumID is null");
                        throw new FaultException<Fault.ValidationFault>(fault);
                    }

                    return areaEnumeratorBO.GetById(areaEnumID);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                   new Fault.UnexpectedServiceFault
                   {
                       ErrorMessage = ex.Message,
                       Source = ex.Source,
                       Target = ex.TargetSite.ToString(),
                       StackTrace = ex.StackTrace
                   },
                   new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }
        public List<AreaEnumerator> GetByEnumID(string enumID)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                    return new List<AreaEnumerator>();
                }
                else
                {
                    if (string.IsNullOrEmpty(enumID))
                    {
                        Fault.ValidationFault fault = new Fault.ValidationFault()
                        {
                            Result = false,
                            Message = "Thiếu tham số",
                            Description = "Thiếu tham số"
                        };

                        throw new FaultException<Fault.ValidationFault>(fault);
                    }

                    return areaEnumeratorBO.GetByEnumID(enumID);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                   new Fault.UnexpectedServiceFault
                   {
                       ErrorMessage = ex.Message,
                       Source = ex.Source,
                       Target = ex.TargetSite.ToString(),
                       StackTrace = ex.StackTrace
                   },
                   new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }
        public void AddAreaEnumerator(AreaEnumerator obj)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                }
                else
                {
                    areaEnumeratorBO.Save(obj, DAL.DAO.TDT.AreaEnumeratorDAO.AddMode);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception"))
               );
            }
        }

        public void DeleteAreaEnumerator(AreaEnumerator obj)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                }
                else
                {
                    areaEnumeratorBO.Delete(obj);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }

        public void EditAreaEnumerator(AreaEnumerator obj)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                }
                else
                {
                    areaEnumeratorBO.Update(obj);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }
    }
}